package com.example.element.module.notification

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import androidx.core.app.NotificationCompat
import com.example.common.base.AbsActivity
import com.example.element.R
import com.example.element.databinding.ActNotificationBinding

class NotificationActivity : AbsActivity<ActNotificationBinding>() {

    companion object {
        @JvmStatic
        fun open(context: Context) {
            context.startActivity(Intent(context, NotificationActivity::class.java))
        }
    }

    override fun getLayoutId() = R.layout.act_notification

    override fun main() {

    }

    override fun initListener() {
        mBinding.btnNotification.setOnClickListener {
            //PendingIntent
            val intent = Intent(this, NotificationActivity::class.java)
            val pi = PendingIntent.getActivity(this, 0, intent, 0)

            //创建通知管理器
            val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            //自动取消notificationManager.cancel(1)
            //设置通知渠道，8.0引入 4个等级：IMPORTANCE_HIGH、IMPORTANCE_DEFAULT、IMPORTANCE_LOW、IMPORTANCE_MIN
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channel = NotificationChannel("normal", "Normal", NotificationManager.IMPORTANCE_DEFAULT)
                notificationManager.createNotificationChannel(channel)
            }
            //通知创建
            val notification = NotificationCompat.Builder(mContext, "normal")
                    .setContentTitle("This is content title")
                    .setContentText("This is content text")
                    //系统状态栏显示的图标
                    .setSmallIcon(R.mipmap.icon_avatar_placeholder)
                    //通知栏显示的图标
                    .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.launcher_icon))
                    //点击，延时执行的Intent
                    .setContentIntent(pi)
                    //点击通知栏会自动取消
                    .setAutoCancel(true)
                    .build()
            notificationManager.notify(1, notification)     //通知Id
        }
    }

    private fun sendGeneralNotice() {

    }

}